package controller

import (
	"hft/Http/Models/request"
	services "hft/Http/Services"
	utils "hft/Http/Utils"
	"hft/tools"
	"strconv"

	"github.com/gin-gonic/gin"
)

type LouPan struct{}

func (*LouPan) CreateLouPan(c *gin.Context) {
	var louPan *request.LouPan

	c.ShouldBindJSON(&louPan)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	err = services.LouPanService.CreateLouPan(louPan, claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)
}

func (*LouPan) GetLouPanList(c *gin.Context) {
	var paginate request.LouPaPaginate

	c.ShouldBind(&paginate)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	data, err := services.LouPanService.GetLouPanList(&paginate, claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(data, c)
}

func (*LouPan) EditLouPan(c *gin.Context) {
	var louPan *request.LouPan

	c.ShouldBindJSON(&louPan)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	err = services.LouPanService.EditLouPan(louPan, claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)
}

func (*LouPan) GetLouPanInfo(c *gin.Context) {
	id, _ := strconv.Atoi(c.DefaultQuery("id", "0"))

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	data, err := services.LouPanService.GetLouPanInfo(id, claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(data, c)

}

func (*LouPan) Delete(c *gin.Context) {

	var IDS *request.IDS

	c.ShouldBindJSON(&IDS)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	err = services.LouPanService.Delete(IDS.IDS, claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)

}

var LouPanController = new(LouPan)
